package admin

import (
	"context"
	"errors"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type PermissionInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPermissionInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PermissionInfoLogic {
	return &PermissionInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PermissionInfoLogic) PermissionInfo(req *types.IdForm) (resp *types.PermissionResp, err error) {
	data := &models.Permission{}
	if !utils.GetDataById(req.Id, data) {
		return nil, errors.New("权限不存在")
	}
	resp = &types.PermissionResp{}
	copier.Copy(resp, data)
	if data.Params != nil {
		utils.JsonToStruct(data.Params.String(), &resp.Params)
	}
	return resp, nil
}
